<template>
    <div>
        <van-form>

            <van-field
                v-model="formData.name"
                name="name"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model="formData.sex"
                name="sex"
                label="性别"
                placeholder="性别"
                :rules="[{ required: true, message: '请填写性别' }]"
            />
            <van-field
                v-model="formData.age"
                name="age"
                label="年龄"
                placeholder="年龄"
                :rules="[{ required: true, message: '请填写年龄' }]"
            />
            <van-field
                v-model="formData.email"
                name="email"
                label="邮箱"
                placeholder="邮箱"
                :rules="[{ required: true, message: '请填邮箱' }]"
            />
            <van-field
                v-model="formData.phone"
                name="phone"
                label="手机号"
                placeholder="手机号"
                :rules="[{ validator: phoneValidator, message: '手机号格式错误' }]"
            />
            <div style="margin: 16px;">
                <van-button @click="onSubmit" round block type="info" native-type="submit">提交</van-button>
            </div>
        </van-form>
    </div>
</template>

<script>

import {getUserInfo, saveUserInfo} from "../../api/user";

export default {
    name: "buyerInfo",
    created() {
        this.getUserInfo();
    },
    data() {
        return {
            formData: {}
        }
    },
    methods: {
        async onSubmit() {
            let result = await saveUserInfo(this.formData);
            console.log(result)
            if(this.utils.resultBool(result)){
                this.$router.back()
            }
        },
        async getUserInfo() {
            let result = await getUserInfo()
            console.log(result)
            if (result.state) {
                console.log(result.data)
                this.formData = result.data
            } else {
                this.$toast("获取用户信息失败！")
            }
        },
        phoneValidator(val) {
            return this.utils.validatePhone(val)
        }
    }
}
</script>

<style scoped>

</style>
